package com.zero.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zero.system.entity.Admin;
import com.zero.system.entity.Company;
import com.zero.system.entity.Models;
import com.zero.system.service.AdminService;
import com.zero.system.service.CompanyService;
import com.zero.system.service.ModelsService;
import com.zero.system.service.RoleService;
import com.zero.system.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/manager")
public class ModelsController {

    @Autowired
    private AjaxResult ajaxResult;
    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    private ModelsService modelsService;

    @Autowired
    private CompanyService companyService;

    /**
     * 跳转机型管理页面
     * @return
     */
    @GetMapping("/models")
    public String models(){
        return "manager/models/modelsList";
    }

    /**
     * 异步加载机型列表
     * @param pageno
     * @param pagesize
     * @return
     */
    @RequestMapping("/modelsList")
    @ResponseBody
    public Object modelsList(@RequestParam(value = "page", defaultValue = "1") Integer pageno,
                                @RequestParam(value = "limit", defaultValue = "5") Integer pagesize,
                                String modelsNo){
        Map<String,Object> paramMap = new HashMap();
        paramMap.put("pageno",pageno);
        paramMap.put("pagesize",pagesize);
        //判断是否为空
        if(!StringUtils.isEmpty(modelsNo)) {
            paramMap.put("modelsNo",modelsNo);
        }
        PageBean<Models> pageBean = modelsService.queryPage(paramMap);
        Map<String,Object> rest = new HashMap();
        rest.put("code", 0);
        rest.put("msg", "");
        rest.put("count",pageBean.getTotalsize());
        rest.put("data", pageBean.getDatas());
        return rest;
    }

    /**
     * 跳转添加或修改机型页面
     * @return
     */
    @GetMapping("/addModels")
    public String addModels(String type, Integer id, Model model){
        List<Company> companyList=companyService.findAll();
        model.addAttribute("companyList",companyList);
        if(type != null && type.equals("edit")){
            Models models = modelsService.findById(id);
            model.addAttribute("models",models);
        }
        return "manager/models/addModels";
    }

    /**
     * 添加机型  修改机型
     * @return
     */
    @PostMapping("/saveModels")
    @ResponseBody
    public AjaxResult saveModels(Models models, HttpServletRequest request){
        String companyId=request.getParameter("companyId");
        Company company=companyService.findById(Integer.parseInt(companyId));
        models.setCompany(company);
        if(models.getId() !=null){
            //修改
            int count = modelsService.update(models);
            if(count > 0){
                ajaxResult.ajaxTrue("修改成功");
            }else{
                ajaxResult.ajaxFalse("修改失败");
            }
        }else{
            //添加
            int count = modelsService.add(models);
            if(count > 0){
                ajaxResult.ajaxTrue("添加成功");
            }else{
                ajaxResult.ajaxFalse("添加失败");
            }
        }
        return ajaxResult;
    }

    /**
     * 删除
     * @param data
     * @return
     */
    @PostMapping("/delModels")
    @ResponseBody
    public AjaxResult delModels(Data data){
        int count = modelsService.delete(data.getIds());
        if(count >= data.getIds().size()){
            ajaxResult.ajaxTrue("删除成功");
        }else{
            ajaxResult.ajaxFalse("删除失败");
        }
        return ajaxResult;
    }

    /**
     * 根据公司ID查找公司信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("findByCompanyId")
    public String findByCompanyId(HttpServletRequest request, HttpServletResponse response) {
        String companyId=request.getParameter("companyId");
        List<Models> modelsList=modelsService.findByCompanyId(Integer.parseInt(companyId));
        JSONArray rows = new JSONArray();
        for (int i=0;i<modelsList.size();i++){
            JSONObject json1 = new JSONObject();
            Models models = modelsList.get(i);
            json1.put("id",models.getId());
            json1.put("modelsNo",models.getModelsNo());
            rows.add(json1);
        }
        JSONObject json2 = new JSONObject();
        json2.put("total", modelsList.size());
        json2.put("rows", rows);
        return json2.toJSONString();
    }

}
